package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xone.xapp.HttpUtils.OnPostSuccessListener;

public class ProductGroupItemUserActivity extends BasicActivity {
	protected final String TAG = ProductGroupItemUserActivity.class.getSimpleName();
    protected LayoutInflater mInflater;
	@Override
	public String getHeaderTitle() {
		return "组团产品详情";
	}
	@Override
	public int getContentView() {
		return R.layout.product_group_item_user;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		String id = (null == bundle) ? "" : bundle.getString("id");
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		if (null == id) {
			Toast.makeText(this, "缺少请求参数", Toast.LENGTH_SHORT).show();
			this.finish();
			return;
		}
		LinearLayout view = (LinearLayout)findViewById(R.id.product_group_item_details_zone);
		showProgress();
		this.mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		new InitDataTask(this, view).execute(id);
	}
	
	public class InitDataTask extends AsyncTask<String, Integer, JSONObject> {
		protected Context context;
		protected LinearLayout view;
		public InitDataTask(Context context, LinearLayout view) {
			this.context = context;
			this.view = view;
		}
		@Override
		protected JSONObject doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			List<NameValuePair> reqparams = new ArrayList<NameValuePair>();
			reqparams.add(new BasicNameValuePair("productGroup.id", params[0]));
			return HttpUtils.httpGet(Constants.APP_URL + "/product/groupJsonItemDetailsForUser.html", reqparams, 
					new HttpUtils.onGetSuccessListener<JSONObject>() {
						@Override
						public JSONObject onSuccess(int status, String result) {
							if (HttpUtils.SC_OK == status) {
								JSONObject jsonObject = JSONUtils.getJSONObjectFromString(result);
								JSONObject productGroup = JSONUtils.getJSONObjectForKey(jsonObject, "productGroup");
								return productGroup;
							} else {
								Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
							}
							return null;
						}
					}, context);
		}
		@Override
		protected void onPostExecute(JSONObject result) {
			super.onPostExecute(result);
			if (null == result) {
				return;
			}
			JSONObject product = result;
			String id = JSONUtils.getStringFromJSONObject(product, "id");
			String checkStatus = JSONUtils.getStringFromJSONObject(product, "checkStatus");
			if (!"1".equals(checkStatus)) {
				view.findViewById(R.id.product_group_opt_info_border).setVisibility(View.VISIBLE);
				view.findViewById(R.id.product_group_opt_info_line).setVisibility(View.VISIBLE);
				//设置数据体,取消组团按钮的操作
				Button cancel = (Button)view.findViewById(R.id.cancel_group);
				cancel.setVisibility(View.VISIBLE);
				cancel.setTag(id);
				cancel.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(final View v) {
						ContextUtils.confirm(getContext(), "确认取消组团?", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								cancelButtonAction(v);
							}
						});
					}
				});
			}
			View linkView = view.findViewById(R.id.productLink);
			linkView.setTag(JSONUtils.getStringFromJSONObject(product, "productId"));
			linkView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String id = (String)v.getTag();
					Intent intent = new Intent(ProductGroupItemUserActivity.this, ProductItemActivity.class);
					intent.putExtra("id", id);
					startActivity(intent);
				}
			});
			int childCount = view.getChildCount();
			for (int i = 0 ; i < childCount; i++) {
				View v = view.getChildAt(i);
				String tag = (String)v.getTag();
				if (v instanceof LinearLayout && StringUtils.isNotBlank(tag)) {
					String value = null;
					if (!product.isNull(tag)) {
						value = JSONUtils.getStringFromJSONObject(product, tag);
					}
					if (null != value) {
						TextView tv = (TextView)view.findViewById(context.getResources().getIdentifier(tag, "id", context.getPackageName()));
						if (null != tv) {
							tv.setText(value);
						}
					}
				}
			}
			hideProgress();
		}
	}
	/**
	 * 取消组团按钮的事件
	 * @param tag
	 */
	protected void cancelButtonAction(final View btn) {
		btn.setEnabled(false);
		showProgress();
		new AsyncTask<String, Void, JSONObject>() {
			@Override
			protected JSONObject doInBackground(String... params) {
				if (isCancelled()) {
					return null;
				}
				List<NameValuePair> param = new ArrayList<NameValuePair>();
				param.add(new BasicNameValuePair("productGroup.id", params[0]));
				return HttpUtils.httpPost(Constants.APP_URL + "/product/doCancelGroupJson.html", param, new OnPostSuccessListener<JSONObject>() {
					@Override
					public JSONObject onSuccess(int status, String result) {
						return JSONUtils.getJSONObjectFromString(result);
					}
				}, getContext());
			}
			protected void onPostExecute(JSONObject result) {
				hideProgress();
				super.onPostExecute(result);
				if (null == result) {
					Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
					btn.setEnabled(true);
					return;
				}
				Toast.makeText(getContext(), "操作成功", Toast.LENGTH_SHORT).show();
				getActivity().finish();
			}
		}.execute((String)btn.getTag());
	}
}
